Skip to content

fix: throw MockException when HttpClient is not mockable#367

Merged
vbreuss merged 4 commits intomainfrom
topic/throw-mockexception-when-httpclient-is-not-mockable
Jan 17, 2026
Merged

fix: throw MockException when HttpClient is not mockable#367
vbreuss merged 4 commits intomainfrom
topic/throw-mockexception-when-httpclient-is-not-mockable

Conversation

@vbreuss
Copy link
Contributor

@vbreuss vbreuss commented Jan 15, 2026

This PR improves error handling for HttpClient mocking by throwing a clear MockException when attempting to setup or verify an HttpClient that was not properly mocked with a mockable HttpMessageHandler.

Key Changes:

  • Replaced fallback logic that attempted to mock HttpClient directly with explicit exception throwing
  • Added validation to ensure ConstructorParameters array has elements before accessing
  • Added comprehensive test coverage for the error scenarios across all HTTP methods

@vbreuss vbreuss self-assigned this Jan 15, 2026
Copilot AI review requested due to automatic review settings January 15, 2026 16:58
@vbreuss vbreuss added the bug Something isn't working label Jan 15, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves error handling for HttpClient mocking by throwing a clear MockException when attempting to setup or verify an HttpClient that was not properly mocked with a mockable HttpMessageHandler.

Changes:

  • Replaced fallback logic that attempted to mock HttpClient directly with explicit exception throwing
  • Added validation to ensure ConstructorParameters array has elements before accessing
  • Added comprehensive test coverage for the error scenarios across all HTTP methods

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Source/Mockolate/Web/HttpClientExtensions.cs Removed unused CastToMockOrThrow helper method
Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs Replaced fallback logic with exception throwing and added length check
Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs Replaced fallback logic with exception throwing and added length check
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs Added tests for string and Uri overloads without mocked handler
Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs Added tests for string and Uri overloads without mocked handler

@vbreuss vbreuss enabled auto-merge (squash) January 15, 2026 16:59
@github-actions
Copy link

github-actions bot commented Jan 15, 2026

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.3 LTS (Noble Numbat)
AMD EPYC 7763 2.64GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.102
[Host] : .NET 10.0.2 (10.0.2, 10.0.225.61305), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean Error StdDev Gen0 Gen1 Allocated
Simple_Mockolate 1.555 μs 0.0253 μs 0.0236 μs 0.2270 - 3.72 KB
Simple_Moq 175.599 μs 0.8487 μs 0.7087 μs 0.4883 - 14.55 KB
Simple_NSubstitute 5.663 μs 0.0174 μs 0.0136 μs 0.5569 0.0076 9.14 KB
Simple_FakeItEasy 6.297 μs 0.0725 μs 0.0678 μs 0.4959 - 8.11 KB

@github-actions
Copy link

github-actions bot commented Jan 15, 2026

Test Results

    14 files  ±  0      14 suites  ±0   6m 41s ⏱️ + 1m 26s
 2 187 tests + 22   2 186 ✅ + 22  1 💤 ±0  0 ❌ ±0 
14 640 runs  +150  14 639 ✅ +150  1 💤 ±0  0 ❌ ±0 

Results for commit aacf9f2. ± Comparison against base commit ecb3045.

♻️ This comment has been updated with latest results.

Copilot AI review requested due to automatic review settings January 15, 2026 17:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated no new comments.

@vbreuss vbreuss force-pushed the topic/throw-mockexception-when-httpclient-is-not-mockable branch from 532255d to aacf9f2 Compare January 17, 2026 16:00
Copilot AI review requested due to automatic review settings January 17, 2026 16:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated no new comments.

@vbreuss vbreuss merged commit dfdf4fd into main Jan 17, 2026
9 of 10 checks passed
@vbreuss vbreuss deleted the topic/throw-mockexception-when-httpclient-is-not-mockable branch January 17, 2026 16:07
@sonarqubecloud
Copy link

@github-actions
Copy link

This is addressed in release v0.52.0.

@github-actions github-actions bot added the state: released The issue is released label Jan 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant